import {createSelector} from 'reselect'

import {
    reqgetnews,
    reqgetgroupon,
    reqgetseckill,
    reqgetindexgoods
} from '../../request/api'
const initState = {
    getnew: [],
    groupon: [],
    seck: [],
    goods: [],
    n: 0

}

const types = {
    CHANGE_GETNEW: 'HOME_CHANGEGETNEW',
    CHANGE_GROUPON: 'HOME_CHANGEGROUPON',
    CHANGE_SECK: 'HOME_CHANGESECK',
    CHANGE_GOODS: 'HOME_CHANGEGOODS',
    CHANGE_N: "HOME_CHANGEN"
}

const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_GETNEW:
            return {
                ...state,
                getnew: action.arr
            }
        case types.CHANGE_GROUPON:
            return {
                ...state,
                groupon: action.arr
            }
        case types.CHANGE_SECK:
            return {
                        ...state,
                        seck: action.arr
                    }
        case types.CHANGE_GOODS:
            return {
                    ...state,
                    goods: action.arr
            }
        case types.CHANGE_N:
            return {
                ...state,
                n:action.n
            }
            default:
            return state
    }
}

export const actions = {
    //修改
    changegetnew: (arr) => ({
        type: types.CHANGE_GETNEW,
        arr
    }),

    changegroupon: (arr) => ({
        type: types.CHANGE_GROUPON,
        arr: arr
    }),
    changeseck: (arr) => ({
        type: types.CHANGE_SECK,
        arr
    }),
    changegoods: arr => ({
        type: types.CHANGE_GOODS,
        arr
    }),
    changen:n=>({type:types.CHANGE_N,n}),
    //请求
    reqgetnew: () => (dispatch, getState) => {
        let { getnew } = getState().home
        if (getnew.length > 0) return;
        reqgetnews().then(res => {
            dispatch(actions.changegetnew(res.data.list))
        })
    },

    reqgegroupon: () => (dispatch, getState) => {
        let { groupon } = getState().home
        if (groupon.length > 0) return;
        reqgetgroupon().then(res => {
            dispatch(actions.changegroupon(res.data.list))
        })
    },

    reqseck: () => (dispatch, getState) => {
        let { seck } = getState().home
        if (seck.length > 0) return;
        reqgetseckill().then(res => {
            dispatch(actions.changeseck(res.data.list))
        })
    },

    reqgoods: () => (dispatch, getState) => {
        let { goods } = getState().home
        if (goods.length > 0) return;
        reqgetindexgoods().then(res => {
            dispatch(actions.changegoods(res.data.list))
        })
    }
}

//导出数据
export const getgetnew = state => state.home.getnew

export const getgroupon = state => state.home.groupon

export const getseck = state => state.home.seck

export const getgoods = state => state.home.goods
export const getn = state => state.home.n



export const getshowgoods=createSelector(
    [getgoods,getn],
    (goods,n) => {
       return goods[n]
    }
)
export default reducer